package com.gxuwz.service.impl;

import com.gxuwz.dao.CourseDao;
import com.gxuwz.entity.Course;
import com.gxuwz.entity.vo.CourseVo;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;

    public PageVo getCourseList(Course course, PageInfo pageInfo,String instID) {
        if(course == null){
            return courseDao.selectCourseAll(null,pageInfo,instID);
        }
        String courseID = course.getCourseID();
        String courseName = course.getCourseName();
        String courseYear = course.getYear();
        String courseTerm = course.getTerm();
        if( (courseID!=null && !courseID.equals("")) || (courseName!= null && !courseName.equals("")) ||
            courseYear!=null && !courseYear.equals("") || courseTerm != null && !courseTerm.equals("")){
            return courseDao.selectCourseAll(course,pageInfo,instID);
        } else {
            return courseDao.selectCourseAll(null,pageInfo,instID);
        }
    }

    public CourseVo getCourseOne(String courseId) {
        CourseVo courseVo = courseDao.selectCourseByID(courseId);
        return courseVo;
    }

    public boolean addCourseInfo(Course course) {
        int i = courseDao.insertCourse(course);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean updateCourseInfo(Course course, String oldId) {
        int i = courseDao.updateCourse(course,oldId);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean deleteCourseOne(String courseId) {
        int i = courseDao.deleteCourseById(courseId);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean deleteCourses(String[] ids) {
        int count = courseDao.deleteCourse(ids);
        boolean flag = count > 0 ? true : false;
        return flag;
    }

    public List<String> selectCourseIds() {
        List<String> ids = courseDao.selectCourseIds();
        return ids;
    }

    public List<String> queryCourseYear() {
        return courseDao.selectCourseYear();
    }

    public List<Course> queryCourseIdAndCourseName(String stuID) {
        List<Course> courseList = courseDao.selectCourseIDAndName(stuID);
        return courseList;
    }

    @Override
    public boolean selectCourseExist(String classId) {
        List<Course> courses = courseDao.selectCourseByClassID(classId);
        if(courses.isEmpty()){
            return true;
        } else {
          return false;
        }
    }
}
